package com.example.springboot2.xdkt.controller;

import com.example.springboot2.xdkt.domain.User;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

//测试http协议的get请求
@RestController
public class GetController {

    private Map<String, Object> params = new HashMap<>();


    /**
     * 功能描述:测试restful协议,从路径中获取字段
     * @param cityId
     * @param userId
     * @return
     */
    @RequestMapping(path = "/demo/{city_id}/{user_id}", method = RequestMethod.GET)
    public Object findUser(@PathVariable("city_id") String cityId,
                           @PathVariable("user_id") String userId) {
        params.clear();
        params.put("cityId", cityId);
        params.put("userId", userId);
        return params;
    }


    /**
     * 功能描述:测试GetMapping
     * @param from
     * @param size
     * @return
     */
    @GetMapping(value = "/v1/page_user1")
    public Object pageUser(int from, int size) {
        params.clear();
        params.put("from", from);
        params.put("size", size);
        return params;
    }


    /**
     * 功能描述: 默认值，是否必须的参数
     */
    @GetMapping(value = "/v1/page_user2")
    public Object pageUserV2(@RequestParam(defaultValue = "0", name = "page", required = true) int from,
                             int size) {
        //如果不给page传输，默认值是0,name属性指定名称，不指定名称就是from
        params.clear();
        params.put("from", from);
        params.put("size", size);
        return params;

    }


    /**
     * 功能描述: bean对象传参 是post方式
     * 注意:  1.注意需要指定http头为content-type为application/json
     *        2.使用body传输数据
     *
     */
    @RequestMapping("/v1/save_user")
    public Object saveUser(@RequestBody User user) {
        params.clear();
        params.put("user", user);
        return params;

    }

    /**
     * 功能描述:测试获取http头信息
     * @param accessToken
     * @param id
     * @return
     */
    @GetMapping("/v1/get_header")
    public Object getHeader(@RequestHeader("access_token") String accessToken, String id) {
        params.clear();
        params.put("access_token", accessToken);
        params.put("id", id);
        return params;
    }


    /**
     * 功能描述:get方式接收httpservlet请求
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/v1/test_rquest")
    public Object testRequest(HttpServletRequest httpServletRequest) {
        params.clear();
        String id = httpServletRequest.getParameter("id");
        params.put("id", id);
        return params;
    }


}
